package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.Server;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ItemSummon_time {
  private static ArrayList<ArrayList<Object>> aData17 = new ArrayList<>();
  
  private static boolean NO_MORE_GET_DATA17 = false;
  
  public static final String TOKEN = ",";
  
  public static void main(String[] a) {
    try {
      while (true)
        Server.main(null); 
    } catch (Exception exception) {
      return;
    } 
  }
  
  public static void forresolvent(L1PcInstance pc, L1ItemInstance item) {
    int itemid = item.getItemId();
    ArrayList aTempData = null;
    int[] iTemp = null;
    if (!NO_MORE_GET_DATA17) {
      NO_MORE_GET_DATA17 = true;
      getData17();
    } 
    int i = 0;
    while (i < aData17.size()) {
      aTempData = aData17.get(i);
      if (((Integer)aTempData.get(0)).intValue() == itemid) {
        if (((Integer)aTempData.get(2)).intValue() != 0) {
          byte class_id = 0;
          String msg = "";
          if (pc.isCrown()) {
            class_id = 1;
          } else if (pc.isKnight()) {
            class_id = 2;
          } else if (pc.isWizard()) {
            class_id = 3;
          } else if (pc.isElf()) {
            class_id = 4;
          } else if (pc.isDarkelf()) {
            class_id = 5;
          } else if (pc.isDragonKnight()) {
            class_id = 6;
          } else if (pc.isIllusionist()) {
            class_id = 7;
          } 
          switch (((Integer)aTempData.get(2)).intValue()) {
            case 1:
              msg = "王族";
              break;
            case 2:
              msg = "骑士";
              break;
            case 3:
              msg = "法师";
              break;
            case 4:
              msg = "妖精";
              break;
            case 5:
              msg = "黑暗妖精";
              break;
            case 6:
              msg = "龙骑士";
              break;
            case 7:
              msg = "幻术士";
              break;
          } 
          if (((Integer)aTempData.get(2)).intValue() != class_id) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("你的职业无法使用" + msg + "的专属道具。"));
            return;
          } 
        } 
        if (((Integer)aTempData.get(3)).intValue() != 0 && 
          pc.getLevel() < ((Integer)aTempData.get(3)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("等级最低需求" + ((Integer)aTempData.get(3)).intValue() + "才可使用"));
          return;
        } 
        if (((Integer)aTempData.get(4)).intValue() != 0 && 
          pc.getLevel() > ((Integer)aTempData.get(4)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("等级最高需求" + ((Integer)aTempData.get(4)).intValue() + "才可使用"));
          return;
        } 
        if (((Integer)aTempData.get(5)).intValue() != 0 && 
          pc.getMeteLevel() < ((Integer)aTempData.get(5)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("转身最低需求" + ((Integer)aTempData.get(5)).intValue() + "才可使用"));
          return;
        } 
        if (((Integer)aTempData.get(6)).intValue() != 0 && 
          pc.getMeteLevel() > ((Integer)aTempData.get(6)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("转身最高限制" + ((Integer)aTempData.get(6)).intValue() + "才可使用"));
          return;
        } 
        int[] materials = (int[])aTempData.get(7);
        int[] counts = (int[])aTempData.get(8);
        int j = 0;
        while (j < materials.length) {
          if (!pc.getInventory().checkItem(materials[j], counts[j])) {
            L1Item temp = ItemTable.get().getTemplate(materials[j]);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp.getName())) + "(" + (
                  counts[j] - pc.getInventory().countItems(temp.getItemId())) + ")"));
            return;
          } 
          j++;
        } 
        if (((Integer)aTempData.get(11)).intValue() != 0 && 
          pc.getMapId() != ((Integer)aTempData.get(11)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("此地图无法使用该道具"));
          return;
        } 
        L1SpawnUtil.spawn(pc, ((Integer)aTempData.get(12)).intValue(), 0, (
            (Integer)aTempData.get(13)).intValue() * 60);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("所招换的时间只有" + ((Integer)aTempData.get(13)).intValue() + "分就会消失"));
        World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("\\fY" + (String)aTempData.get(14)));
        int k = 0;
        while (k < materials.length) {
          if (((Integer)aTempData.get(9)).intValue() != 0 && ((Integer)aTempData.get(9)).intValue() == 1)
            pc.getInventory().consumeItem(materials[k], counts[k]); 
          k++;
        } 
        L1ItemInstance item2 = pc.getInventory().findItemId(((Integer)aTempData.get(0)).intValue());
        if (((Integer)aTempData.get(10)).intValue() != 0 && ((Integer)aTempData.get(10)).intValue() == 1)
          pc.getInventory().removeItem(item2, 1L); 
      } 
      i++;
    } 
  }
  
  private static void getData17() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_道具召唤时间");
      ArrayList aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("itemid")));
          aReturn.add(1, rset.getString("note"));
          aReturn.add(2, new Integer(rset.getInt("check_Class")));
          aReturn.add(3, new Integer(rset.getInt("check_Level_min")));
          aReturn.add(4, new Integer(rset.getInt("check_Level_max")));
          aReturn.add(5, new Integer(rset.getInt("check_power_min")));
          aReturn.add(6, new Integer(rset.getInt("check_power_max")));
          if (rset.getString("check_item") != null && !rset.getString("check_item").equals("") && 
            !rset.getString("check_item").equals("0")) {
            aReturn.add(7, getArray(rset.getString("check_item"), ",", 1));
          } else {
            aReturn.add(7, null);
          } 
          if (rset.getString("check_count") != null && !rset.getString("check_count").equals("") && 
            !rset.getString("check_count").equals("0")) {
            aReturn.add(8, getArray(rset.getString("check_count"), ",", 1));
          } else {
            aReturn.add(8, null);
          } 
          aReturn.add(9, new Integer(rset.getInt("dele_checkitem")));
          aReturn.add(10, new Integer(rset.getInt("dele_itemid")));
          aReturn.add(11, new Integer(rset.getInt("check_mapid")));
          aReturn.add(12, new Integer(rset.getInt("npcid")));
          aReturn.add(13, new Integer(rset.getInt("npcid_time")));
          aReturn.add(14, rset.getString("world_message"));
          aData17.add(aReturn);
        }  
      SQLUtil.close(rset);
      SQLUtil.close(stat);
      SQLUtil.close(con);
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
